﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace Despatching
{
    static class Utils
    {
        public static bool TryAddCountOrReset(this CountdownEvent @this, int count = 1)
        {
            lock(@this)
            {
                if(@this.TryAddCount(count))
                    return true;

                @this.Reset(count);
                return false;
            }
        }

        public static bool Remove<T>(this LinkedList<T> @this, Predicate<T> predicate)
        {
            for(var node = @this.First; node != null;)
            {
                var n = node;
                node = node.Next;

                if(predicate(n.Value))
                {
                    @this.Remove(n);
                    return true;
                }
            }
            return false;
        }
    }
}
